﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;

namespace Estimate.Common.Estimate
{
    /// <summary>
    /// 气温预报类
    /// </summary>
    public class TempForecast
    {
        private uint _Str;
        /// <summary>
        /// 预报站号
        /// </summary>
        public string FStNum = "";

        /// <summary>
        /// 预报值
        /// </summary>
        public float FValue = -100f;

        /// <summary>
        /// 实况站号,如果与预报站号一致则为空
        /// </summary>
        public string RStNum = "";

        /// <summary>
        /// 实况值  
        /// </summary>
        public float RValue;


        /// <summary>
        /// 温差
        /// </summary>
        private float _offset = -1000;

        public float Offset
        {
            get { return _offset; }
        }


        public readonly int EmptyForecast = -100;
        public readonly int LackForecast = -101;
        public readonly int LackReal = -102;
        public readonly int ForecastError = -103;
        public readonly int RealError = -104;
        public readonly int EmptyReal = -105;

        public string SemiResult
        {
            get
            {
                if (!_isInitialized)
                    CheckOffset();
                return string.Format("{0};{1}", FStNum, Offset);
            }
            
        }

        /// <summary>
        /// 由于semi入库后，再从semi恢复时,rstnum, rvalue 等值都已经丢失掉。对于准确评估值的状态有影响
        /// 所以如果 _isInitialized 被指定为 True,则相关条件不再进行判断。保持原状
        /// </summary>
        private bool _isInitialized = false;

        public void SetSemiResult(string value)
        {
            _isInitialized = true;
            string[] oserial = Regex.Split(value, @"\;", RegexOptions.IgnoreCase);

            FStNum = oserial[0];

            float result;
            if (oserial.Length < 2)
            {
                result = 0;
            }
            float.TryParse(oserial[1].ToString(), out result);
            _offset = result;
        }

        /// <summary>
        /// 根据实际情况，计算值
        /// </summary>
        public void CheckOffset()
        {
            _isInitialized = true;
            if (FValue == -100f)
            {//漏预报
                _offset = LackForecast;
            }
            else if (RStNum == "")
            {//漏实况
                _offset = LackReal;
            }
            else if (FValue == 999.9f)
            {
                _offset = EmptyForecast;
            }
            else if (RValue == 9999)
            {
                _offset = EmptyReal;
            }
            else if (FValue > 45f || FValue < -15f)
            {
                _offset = ForecastError;
            }
            else if (RValue > 45f || RValue < -15f)
            {
                _offset = RealError;
            }            
            else
            {
                _offset = FValue - RValue;
            }
        }

        /// <summary>
        /// 如果实况值为null。则为缺实况
        /// </summary>
        public void SetLackReal()
        {
            _isInitialized = true;
            _offset = LackReal;
        }

        public bool IsEmptyForecast()
        {
            return _offset == EmptyForecast;            
        }

        public bool IsLackForecast()
        {
            return _offset == LackForecast;
        }

        public bool IsLackReal()
        {
            return _offset == LackReal;            
        }

        public bool IsForecastError()
        {
            return _offset == ForecastError;            
        }

        public bool IsRealError()
        {
            return _offset == RealError;            
        }
        public bool IsEmptyReal()
        {
            return _offset == EmptyReal ;
        }     
    }
}
